package LiCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class No49 {
    public static void main(String[] args) {
        String[] strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result = groupAnagrams(strs);
        result.forEach(System.out::println);
    }

    private static List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> hashMap = new HashMap<>();
        for (String str : strs) {
            char[] array = str.toCharArray();
            Arrays.sort(array);
            String key = new String(array);
            if (!hashMap.containsKey(str)) {
                hashMap.put(key, new ArrayList());
            }
            hashMap.get(key).add(str);

        }

        return new ArrayList<List<String>>(hashMap.values());
    }
}